package com.ruoyi.ysVideo.utls;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.ysVideo.domain.MyToken;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @program: class by cxf
 * @author: AbrahamBill
 * @date: 2022/9/2
 **/
public class MyTools {
    // 获取token，请传入app_key 和 app_secret两个参数
    // 返回MyToken对象，包含每个接口都需要的token和此token的过期时间
    public static MyToken get_access_token(String app_key,String app_secret){
//        String app_key = "0140139b6f0b4d0b885cd9a924e3ff0c";
//        String app_secret = "10d9bdef05d25e6ab845cb218fe6590c";
        MyToken myToken = new MyToken();
        try{
            RestTemplate restTemplate = new RestTemplate();
            String post_url="https://open.ys7.com/api/lapp/token/get";
            MultiValueMap<String,Object> param = new LinkedMultiValueMap<>();
            param.add("appKey",app_key);
            param.add("appSecret",app_secret);
            JSONObject ret = restTemplate.postForObject(post_url,param,JSONObject.class);
            myToken = JSONObject.parseObject(JSONObject.toJSONString(ret.get("data")),MyToken.class);

        }catch (Exception e){
            System.out.println(e);
        }
        return myToken;
    }

    public static MultipartFile createMultiPartFileByPath(String file_dir, String file_name) throws IOException {
        File file= new File(file_dir + File.separator + file_name);
        FileInputStream input = new FileInputStream(file);
        // 构造MultipartFile对象
        return new MockMultipartFile(
                file.getName(),
                file.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(),
                input
        );
    }

    public static boolean isWindowsSystem(){
        String os = System.getProperty("os.name").toLowerCase();
        return os.contains("win");
    }
}
